﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.segments.data_type;
using ConsoleApplication1.segments.field;

namespace ConsoleApplication1.segments
{
    class PV1   // lesa matgrbtysh
    {
        public StringBuilder PV1_ID { get; set; }
        public StringBuilder patient_class { get; set; }
        public PL Assigned_Patient_Location { get; set; }
        public StringBuilder Admission_Type { get; set; }
        public CX Pre_admit_Number { get; set; }
        public PL Prior_Patient_Location { get; set; }
        public CN Attending_Doctor { get; set; }
        public CN Referring_Doctor { get; set; }
        public CN Consulting_Doctor { get; set; }
        public StringBuilder Hospital_Service { get; set; }
        public PL Temporary_Location { get; set; }
        public StringBuilder Pre_admit_test_Indicator { get; set; }
        public StringBuilder Readmission_Indicator { get; set; }
        public StringBuilder Admit_Source { get; set; }
        public StringBuilder Ambulatory_Status { get; set; }
        public StringBuilder VIP_Indicator { get; set; }
        public CN Admitting_Doctor { get; set; }
        public StringBuilder 	Patient_Type { get; set; }
        public CX Visit_Number { get; set; }

        enum segments { DG1, EVN, MSH, OBX, ORC, PID, PV1, RXO, NTE, OBR }

        string check(string source, int temp)
        {

            if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.MSH.ToString())
                return segments.MSH.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.DG1.ToString())
                return segments.DG1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.EVN.ToString())
                return segments.EVN.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.NTE.ToString())
                return segments.NTE.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBR.ToString())
                return segments.OBR.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.OBX.ToString())
                return segments.OBX.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.ORC.ToString())
                return segments.ORC.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PID.ToString())
                return segments.PID.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.PV1.ToString())
                return segments.PV1.ToString();
            else if (source[temp].ToString() + source[temp + 1].ToString() + source[temp + 2].ToString() == segments.RXO.ToString())
                return segments.RXO.ToString();

            return null;
        }

        public PV1()
        {
            PV1_ID=new StringBuilder() ;
            patient_class =new StringBuilder() ;
            Admission_Type =new StringBuilder() ;
            Hospital_Service =new StringBuilder() ;
            Pre_admit_test_Indicator =new StringBuilder() ;
            Readmission_Indicator=new StringBuilder() ;
            Admit_Source=new StringBuilder() ;
            Ambulatory_Status =new StringBuilder() ;
            VIP_Indicator=new StringBuilder() ;
            Patient_Type =new StringBuilder() ;
        }
        public int fill(string source, int temp)
        {
            int i, x = 0;
            for (i = temp; i < source.Length; i++)
            {
                if (check(source, i) != null)
                {
                    PV1_ID.Capacity = PV1_ID.Length;
                    patient_class.Capacity = patient_class.Length;
                    Admission_Type.Capacity = Admission_Type.Length;
                    Hospital_Service.Capacity = Hospital_Service.Length;
                    Pre_admit_test_Indicator.Capacity = Pre_admit_test_Indicator.Length;
                    Readmission_Indicator.Capacity = Readmission_Indicator.Length;
                    Admit_Source.Capacity = Admit_Source.Length;
                    Ambulatory_Status.Capacity = Ambulatory_Status.Length;
                    VIP_Indicator.Capacity = VIP_Indicator.Length;
                    Patient_Type.Capacity = Patient_Type.Length;
                    return i - 1;
                }
               else if (source[i] == '|')
                {
                    if (x == 19)
                    {
                        PV1_ID.Capacity = PV1_ID.Length;
                        patient_class.Capacity = patient_class.Length;
                        Admission_Type.Capacity = Admission_Type.Length;
                        Hospital_Service.Capacity = Hospital_Service.Length;
                        Pre_admit_test_Indicator.Capacity = Pre_admit_test_Indicator.Length;
                        Readmission_Indicator.Capacity = Readmission_Indicator.Length;
                        Admit_Source.Capacity = Admit_Source.Length;
                        Ambulatory_Status.Capacity = Ambulatory_Status.Length;
                        VIP_Indicator.Capacity = VIP_Indicator.Length;
                        Patient_Type.Capacity = Patient_Type.Length;

                        return i;
                    }
                    else
                    {
                        x++;
                        continue;
                    }
                }
                else
                {
                    if (x == 1)  // passing prameter of set id - PID
                    {
                        if (source[i] != '|')
                        {
                         
                            PV1_ID.Insert(PV1_ID.Length, source[i]);
                        }
                    }
                    else if (x == 2)
                    {
                        if (source[i] != '|')
                        {
                           
                            patient_class.Insert(patient_class.Length, source[i]);
                        }
                    }
                    else if (x == 3)
                    {
                        if (source[i] != '|')
                        {
                           
                            i=Assigned_Patient_Location.fill(source, i);
                        }
                    }
                    else if (x == 4)
                    {
                        if (source[i] != '|')
                        {
                            
                            Admission_Type.Insert(Admission_Type.Length, source[i]);
                        }
                    }
                    else if (x == 5)
                    {
                        if (source[i] != '|')
                        {
                            
                           i= Pre_admit_Number.fill(source, i);


                        }
                    }
                    else if (x == 6)
                    {
                        if (source[i] != '|')
                        {
                           
                           i= Prior_Patient_Location.fill(source, i);

                        }
                    }
                    else if (x == 7)
                    {
                        if (source[i] != '|')
                        {
                          i=  Attending_Doctor.fill(source, i);
                        }
                    }
                    else if (x == 8)
                    {
                        if (source[i] != '|')
                        {
                            
                           i= Referring_Doctor.fill(source, i);
                        }
                    }
                    else if (x == 9)
                    {
                        if (source[i] != '|')
                        {
                           
                           i= Consulting_Doctor.fill(source, i);
                        }
                    }

                    else if (x == 10)
                    {
                        if (source[i] != '|')
                        {
                            
                            Hospital_Service.Insert(Hospital_Service.Length, source[i]);
                        }
                    }

                    else if (x == 11)
                    {
                        if (source[i] != '|')
                        {
                           
                           i= Temporary_Location.fill(source, i);
                        }
                    }
                    else if (x == 12)
                    {
                        if (source[i] != '|')
                        {                       
                            Pre_admit_test_Indicator.Insert(Pre_admit_test_Indicator.Length, source[i]);
                        }
                    }
                    else if (x == 13)
                    {
                        if (source[i] != '|')
                        {

                            Readmission_Indicator.Insert(Readmission_Indicator.Length, source[i]);
                        }
                    }
                    else if (x == 14)
                    {
                        if (source[i] != '|')
                        {

                            Admit_Source.Insert(Admit_Source.Length, source[i]);
                        }
                    }
                    else if (x == 15)
                    {
                        if (source[i] != '|')
                        {
 
                            Ambulatory_Status.Insert(Ambulatory_Status.Length, source[i]);
                        }
                    }
                    else if (x == 16)
                    {
                        if (source[i] != '|')
                        {

                            VIP_Indicator.Insert(VIP_Indicator.Length, source[i]);
                        }
                    }
                    else if (x == 17)
                    {
                        if (source[i] != '|')
                        {

                           i= Admitting_Doctor.fill(source, i);
                        }
                    }
                    else if (x == 18)
                    {
                        if (source[i] != '|')
                        {

                            Patient_Type.Insert(Patient_Type.Length, source[i]);
                        }
                    }
                    else if (x == 19)
                    {
                        if (source[i] != '|')
                        {

                           i= Visit_Number.fill(source, i);
                           return i;
                        }
                    }
                }
            }
            return i;
        }


        public override string ToString()
        {
            string PV1_string = "PV1";

            if (PV1_ID.Length != 0)
                PV1_string += "|" + PV1_ID;
            if (patient_class.Length != 0)
                PV1_string += "|" + patient_class;
            if (Assigned_Patient_Location != null)
                PV1_string += "|" + Assigned_Patient_Location.ToString();
            if (Admission_Type.Length != 0)
                PV1_string += "|" + Admission_Type;
            if (Pre_admit_Number != null)
                PV1_string += "|" + Pre_admit_Number.ToString();
            if (Prior_Patient_Location != null)
                PV1_string += "|" + Prior_Patient_Location.ToString();
            if (Attending_Doctor != null)
                PV1_string += "|" + Attending_Doctor.ToString();
            if (Referring_Doctor != null)
                PV1_string += "|" + Referring_Doctor.ToString();
            if (Consulting_Doctor != null)
                PV1_string += "|" + Consulting_Doctor.ToString();
            if (Hospital_Service.Length != 0)
                PV1_string += "|" + Hospital_Service;
            if (Temporary_Location != null)
                PV1_string += "|" + Temporary_Location.ToString();
            if (Pre_admit_test_Indicator.Length != 0)
                PV1_string += "|" + Pre_admit_test_Indicator;
            if (Readmission_Indicator.Length != 0)
                PV1_string += "|" + Readmission_Indicator;
            if (Admit_Source.Length != 0)
                PV1_string += "|" + Admit_Source;
            if (Ambulatory_Status.Length != 0)
                PV1_string += "|" + Ambulatory_Status;
            if (VIP_Indicator.Length != 0)
                PV1_string += "|" + VIP_Indicator;
            if (Admitting_Doctor != null)
                PV1_string += "|" + Admitting_Doctor.ToString();
            if (Patient_Type.Length != 0)
                PV1_string += "|" + Patient_Type;
            if (Visit_Number != null)
                PV1_string += "|" + Visit_Number.ToString();
           
            return PV1_string;
        }


    }
}
